package cn.nextProject.service.rayfile;

import cn.nextProject.configs.MinioConfig;
import cn.nextProject.entity.FileResult;
import cn.nextProject.entity.Result;
import cn.nextProject.service.minio.MinioService;
import io.minio.MinioClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 共享网盘文件服务层
 */
@Slf4j
@Service
public class FileService {

    @Autowired
    private MinioClient minioClient;

    @Autowired
    private MinioConfig minioConfig;
    @Autowired
    private MinioService minioService;
    @Autowired
    private BucketService bucketService;

    public Map<Integer, String> createFolder(FileResult fileResult) throws Exception {
        Map<Integer, String> result = new HashMap<>();
        if (ObjectUtils.isEmpty(fileResult)){
            result.put(0,"创建文件不能为空！");
            return result;
        }

        boolean exist = minioService.isExist(fileResult.getBucketName());
        if (!exist){
            result.put(0,"桶不存在！");
            return result;
        }

        boolean file = minioService.isFile(fileResult.getBucketName(), fileResult.getLastPath() + "/" + fileResult.getName() + "/");
        if (file){
            result.put(0,"文件已存在！");
            return result;
        }

        minioService.createFolder(fileResult);

        result.put(1,"创建成功");
        return result;
    }

    /**
     * 重命名
     * @param buckName
     * @param oldPath
     * @param newPath
     * @return
     * @throws Exception
     */
    public Map<Integer, String> renameFile(String buckName,String oldPath,String newPath) throws Exception {
        Map<Integer, String> result = new HashMap<>();
        boolean oldFile = minioService.isFile(buckName, oldPath);
        if (!oldFile){
            result.put(0,"修改的文件不存在！");
            return result;
        }

        boolean newFile = minioService.isFile(buckName, newPath);
        if (newFile){
            result.put(0,"文件名称已存在！");
            return result;
        }

        minioService.renameFile(buckName, oldPath, newPath);

        result.put(1,"重命名成功");
        return result;

    }

}
